layui.define(['jquery','form', 'YsModal'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id_panel = c.id_panel;
			t.c.id_table = 'id_table_' + s;
			
			t.c.title = c.title; // 标题
			t.c.colArr = c.colArr; // 列配置数组
			t.c.btnArr = c.btnArr; // 按钮数组
			t.c.data = c.data; // 数据
			t.c.gridId = c.gridId; // 所属子列表菜单ID
		},
        load:function () { // 加载控件
        	var h = ``
        		+ `<div class="layui-form layui-form-item" style="background-color: #ffffff;">`
    				+ `<label style="font-weight: 700;font-size: 18px; padding: 5px;">` + this.c.title + `</label>`
        		+ `</div>`
        		+ `<div class="layui-table-tool" style="padding: 0; min-height: 0; background-color: #ffffff;">`
        			+ `<div class="layui-table-tool-temp">`
        				+ `<div class="layui-btn-container">`
        					+ this.loadBtn()
        				+ `</div>`
    				+ `</div>`
    			+ `</div>`
            	+ `<div class="layui-form layui-form-item" pane>`
            		+ `<div id="`+this.c.id_table+`">`
            			+ this.loadTable()
            		+ `</div>`
        		+ `</div>`
    		+ `</div>`;
            $('#'+this.c.id_panel).html(h);
            this.listenBtn();
        },
        loadBtn:function () { // 加载按钮
        	var t = this;
        	var btnArr = t.c.btnArr;
        	var h = ``;
        	for (var i = 0; i < btnArr.length; i++) {
				var btn = btnArr[i];
				h += `<a class="layui-btn layui-btn-normal layui-btn-sm" data-filter="ys-sub-grid-btn-event" data-event="` + btn.event + `"><i class="iconfont ` + btn.icon + `"></i>` + btn.name + `</a>`
			}
            return h;
        },
        loadTable:function () { // 加载表格
            return ``
        	+ `<div class="layui-table-box">`
        		+ `<div class="layui-table-header" style="height: 300px; max-height: 300px; overflow: auto; background-color: #ffffff;">`
	            	+ `<table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="grid" lay-even="" style="margin: 0;">`
	            		+ `<thead>`
	            			+ this.loadTableOfThead()
	            		+ `</thead>`
	            		+ `<tbody>` + this.loadTableOfTbody()+`</tbody>`
	        		+ `</table>`
        		+ `</div>`
    		+ `</div>`;
        },
        loadTableOfThead:function () { // 加载表头
        	var t = this;
            var h = `<thead>`;
            h +=`<tr>`;
            for (var i = 0; i < t.c.colArr.length; i++) {
            	var col = t.c.colArr[i];
                h += `<th style="width:5%; text-align: center; color: #000000;">` + col.title + `</th>`;
			}
            
            if(this.c.onProcess == 0) {
                h += `<th style="width:4%; text-align: center; color: #000000;">操作</th>`;
            }
            h += `</tr>`;
            h + `</thead>`;
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = `<tbody>`;
            for(var i=0;t.c.data && i<t.c.data.length;i++){
	            h += `<tr>`;
	            for (var j = 0; j < t.c.colArr.length; j++) {
	            	var col = t.c.colArr[j];
	            	console.log(t.c.data[i]);
	            	console.log(col);
		            h += `<td style="text-align:center; padding: 0;"><span class="text_overflow_ellipsis" title="` + t.c.data[i][col.field] + `">` + t.c.data[i][col.field] + `</span></td>`;
				}
                h+= `</tr>`;
            }
            
            h += `</tbody>`;
			return h;
        },
        listenFormItem:function () { // 添加监听
            var t = this;
            
            var domTable = $('#' + t.c.tableId);
			// 监听表格中各个输入框
            domTable.find('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});
			
			// 初始化左上角按钮工具栏监听
			layui.YsSubGridBtn.init({gridId: t.c.gridId, id_table_main: t.c.id_table_main, gridObj: t});
            
            // 表单控件重新加载
        	form.render();
        },
        updateRowAfterSelect:function (index, data) { // 更新数据行
        	var t = this;
        	
        	var rowCurrent = t.c.data[index];
        	rowCurrent.runningIdTwo = data.id;
        	rowCurrent.vgsbmFromTwo = data.vGsBm;
        	rowCurrent.vgsjcFromTwo = data.vGsJc;
        	rowCurrent.vgsbmToTwo = data.vGsBmP;
        	rowCurrent.vgsjcToTwo = data.vGsJcP;
        	rowCurrent.vjylxTwo = data.vJyLx;
        	rowCurrent.fjeTwo = data.vJe;
        	rowCurrent.vbzTwo = data.vBz;
			
			return this.reload(t.c);
			
        },
        removeRow:function (index) { // 重新加载表格
        	var t = this;
        	t.c.data.splice(index, 1);
			return t.reload(t.c);
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        },
        listenBtn:function () { // 添加按钮监听
            var t = this;
			
            // 监听“各个”按钮
            $('#' + t.c.id_panel).find('a[data-filter="ys-sub-grid-btn-event"]').on('click',function (e) {
            	var event = $(this).attr('data-event');
            	if(t.c.gridId == 1) {
            		t.listenBtnOf1(event);
            	}
            });
        },
        listenBtnOf1: function(event) {
        	var t = this;
        	switch (event) {
			case 'event-to-select':
				layui.YsModal.init({modalKey: 'TCW_AccountDetailedForCompanyForRunningBack', type: 'checkbox', uniqueKey: 'id', parentObj: t, selectedData: [{'id': 1}]});
				break;

			default:
				break;
			}
        },
        matchValuesAfterModalSelect: function(colCode, datas) { // 通用回调函数
        	console.log(colCode);
        	console.log(datas);
        	if (this.c.gridId == 1) {
        		this.afterSelectOf1(datas);
			}
        },
        afterSelectOf1:function (datas) { // 添加数据行
        	console.log(datas);
        	var t = this;
        	var ids = '';
        	for (var i = 0; i < datas.length; i++) {
				ids = ids + datas[i].id + ',';
			}

			var indexOfLoading = layer.load(1, {
				shade: [0.5,'#000000'] //0.1透明度的白色背景
			});
			
			$.ajax({
				type: "post",
				url: "/api/inter/tcwRunningBack/checkForAdd",
				data: {ids : ids.substring(0, ids.length - 1)},
				dataType: "json",
				success: function (res) {
					if(res.code != 0) {
						layer.msg(res.msg);
					} else {
						console.log(res.data);
						t.addRows(res.data.rows);
					}
					layer.close(indexOfLoading);
				},
				error: function () {
				    alert("加载失败！")
				}
			});
        }
	};
	exports('YsSubGrid', object);
});